package com.zhixin.fans.ks.ui.history;


import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhixin.fans.ks.R;
import com.zhixin.fans.ks.api.CommonApi;
import com.zhixin.fans.ks.api.res.OrderHistory;
import com.zhixin.fans.ks.manager.UserInfoManager;
import com.zhixin.fans.ks.ui.base.BaseDaggerFragment;

import org.eenie.common.subscribes.ProgressSubscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class OrderHistoryFragment extends BaseDaggerFragment implements BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.rv_order)
    RecyclerView mRvOrder;
    OrderHistoryAdapter mOrderHistoryAdapter;
    List<OrderHistory> mOrders = new ArrayList<>();

//    CoupleApi mCoupleApi;

    UserInfoManager mUserInfoManager;


    CommonApi mCommonApi;


    int page = 1;


    public OrderHistoryFragment(String title) {
        super(title);
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_order_history;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mCommonApi = getApplicationComponent().getApiManager().create(CommonApi.class);
        mUserInfoManager = getCoupleAppComponent().getUserInfoManager();
    }

    @Override
    public void initUI(View rootView) {
        ButterKnife.bind(this, rootView);
        mRvOrder.setLayoutManager(new LinearLayoutManager(mContext));
        mRvOrder.setAdapter(mOrderHistoryAdapter = new OrderHistoryAdapter(mOrders));

        mOrderHistoryAdapter.setEnableLoadMore(true);
        mOrderHistoryAdapter.setOnLoadMoreListener(this, mRvOrder);

    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        fetchHistory(page);
    }

    private void fetchHistory(int index) {
        mCommonApi.fetchOrders(index)
                .subscribe(new ProgressSubscriber<List<OrderHistory>>() {
                    @Override
                    public void onNext(List<OrderHistory> orderHistories) {
                        if (orderHistories.size() < 20) {
                            mOrderHistoryAdapter.loadMoreEnd(true);
                        } else {
                            mOrderHistoryAdapter.loadMoreComplete();
                        }
                        page++;
                        mOrders.addAll(orderHistories);
                        mOrderHistoryAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }

    @Override
    public void onLoadMoreRequested() {
        fetchHistory(page);
    }
}
